﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CasaBella.Entities;
using CasaBella.BL;
using Microsoft.Practices.Unity;

namespace CasaBella.UI
{
    public partial class FrmProveedor : Form
    {
        [Dependency]
        public IProveedorBL proveedorBL { get; set; }

        [Dependency]
        public ITelefonoProveedorBL teleProvBL { get; set; }

        public FrmProveedor()
        {
            InitializeComponent();
            this.BuilUp();
        }

        bool isExtend;

        public void Extend()
        {
            isExtend = true;
        }

        private void FrmProveedor_Load(object sender, EventArgs e)
        {
            cargaData();
        }

        public void cargaData()
        {
            pROVEEDORBindingSource.DataSource = proveedorBL.GetAllFromProveedor();
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            pROVEEDORBindingSource.DataSource = proveedorBL.GetAllFromProveedorByCriterio(TxtCriterio.Text);
            if (pROVEEDORBindingSource.Count == 0)
            {
                MessageBox.Show("Proveedor no existente.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void TxtCriterio_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                pROVEEDORBindingSource.DataSource = proveedorBL.GetAllFromProveedorByCriterio(TxtCriterio.Text);
                if (pROVEEDORBindingSource.Count == 0)
                {
                    MessageBox.Show("Proveedor no existente.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            FrmMttoProveedor frm = new FrmMttoProveedor();
            frm.NuevoProveedor();
            frm.ShowDialog();
            cargaData();
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            var proveedor = pROVEEDORBindingSource.Current as PROVEEDOR;

            if (proveedor != null)
            {
                FrmMttoProveedor frm = new FrmMttoProveedor();
                frm._proveedor = proveedor;
                frm.EditarProveedor();
                frm.ShowDialog();
                cargaData();
            }
            else
            {
                MessageBox.Show("Debe seleccionar un Proveedor.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void TxtCriterio_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (rbtnRazSocial.Checked == true)
            {
                var amberson = Convert.ToString("&");
                var par1 = Convert.ToString("(");
                var par2 = Convert.ToString(")");
                var punto = Convert.ToString(".");
                var guion = Convert.ToString("-");
                var coma = Convert.ToString(",");


                string cadena = amberson + par1 + par2 + punto + guion + coma;

                if (Char.IsLetter(e.KeyChar) || cadena.Contains(e.KeyChar) || Char.IsNumber(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsControl(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsSeparator(e.KeyChar))
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                }
            }
            else if (rbtnRuc.Checked == true)
            {
                if (Char.IsNumber(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsControl(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsSeparator(e.KeyChar))
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                }
            }
        }

        private void rbtnRazSocial_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnRuc.Checked == true)
            {
                TxtCriterio.Text = "";
            }
            else if (rbtnRazSocial.Checked == true)
            {
                TxtCriterio.Text = "";
            }
        }

        public PROVEEDOR currentProveedor { get; set; }

        private void pROVEEDORRadGridView_DoubleClick(object sender, EventArgs e)
        {
            if (isExtend == true)
            {
                currentProveedor = pROVEEDORBindingSource.Current as PROVEEDOR;
                this.Close();
            }
        }

        private void pROVEEDORRadGridView_CurrentCellChanged(object sender, Telerik.WinControls.UI.CurrentCellChangedEventArgs e)
        {
            try
            {
                var currentProveedor = pROVEEDORBindingSource.Current as PROVEEDOR;
                for (int count = 0; count < pROVEEDORRadGridView.Rows.Count; count++)
                {
                    if (currentProveedor != null)
                    {
                        tELEFONOSPROVEEDORBindingSource.DataSource = teleProvBL.GetAllFromTelefonoProveedorByProveedor(currentProveedor.Ruc);
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Error");
            }
        }
    }
}
